package junior.Recursion递归;

public class 全排列_递归DFS {

    static int Number_Of_Elements = 3, top = 0, total = 0;
    static int[] Elements= {1,2,3};
    static int[] statck = new int[Number_Of_Elements]; //自定义栈，top指空，先赋值后加加（先减减，后置空）
    static int[] visit  = new int[Number_Of_Elements];
    public static void main(String[] args) {
        long t1 = System.currentTimeMillis();
        init(visit);
        DFS(0);
        System.out.println(total);
        long t2 = System.currentTimeMillis();
        System.out.println("时间："+(t2-t1));
    }
    // 初始化标记数组，0：未访问，1表示访问
    public static void init(int[] visit){
        for (int i = 0; i < visit.length; i++) {
            visit[i] = 0;
        }
    }
    public static void DFS (int treeDepth){
        if(treeDepth == Elements.length){
            for (int i = 0; i < Elements.length; i++) {
                System.out.print(statck[i]+"\t");
            }
            System.out.println();
            total++;
            return;
        }
        for (int i = 0; i < Elements.length; i++) {
            if(visit[i] == 0){
                visit[i] = 1;
                statck[top++] = Elements[i];
                DFS(treeDepth+1);
                top--;
                visit[i]=0;
            }
        }
    }
}
